#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <vector>
#include <math.h>
using namespace cv;
#include <iostream>
using namespace std;


int Targeting_algorithms() {
    VideoCapture video;
    video.open("../test.avi");
    if (!video.isOpened())  //打不开，报错
    {
        cout << "could not open the Video!" << endl;
        system("pause");
        return -1;
    }

    const char *windowsName = "original img";
    int k = -1;
    while (true) {
        Mat img, dstimg, hsvImage;
       // Mat element1 = getStructuringElement(MORPH_ELLIPSE, Size(1, 1));  //定义开运算的核
       // Mat element2 = getStructuringElement(MORPH_ELLIPSE, Size(2, 2));  //定义开运算的核
        Mat element = getStructuringElement(MORPH_RECT, Size(2, 2));  //定义开运算的核
        Mat element2 = getStructuringElement(MORPH_RECT, Size(6, 6));
        bool ok = video.read(img);
        if (!ok)
            break;
        cvtColor(img, hsvImage, COLOR_BGR2HSV);             //图像灰度处理
        inRange(hsvImage, Scalar(100, 43, 46), Scalar(124, 255, 255), dstimg);
       // morphologyEx(dstimg, dstimg, MORPH_CLOSE, element);
      //  morphologyEx(dstimg, dstimg, MORPH_OPEN, element1);
       // morphologyEx(dstimg, dstimg, MORPH_OPEN, element2);
      //  morphologyEx(dstimg, dstimg, MORPH_OPEN, element);
        morphologyEx(dstimg, dstimg, MORPH_CLOSE, element2);
        vector<vector<Point>> contours;
        vector<Vec4i> hierarchy;
        ::findContours(dstimg, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);

        for (int index = 0; index < contours.size(); index++) {
            Scalar color(0, 255, 255);
            vector<Point> points = contours[index];
            RotatedRect box = minAreaRect(Mat(points));
            Point2f vertex[4];
            box.points(vertex);
            double width, lenth;
            width = sqrt(pow((vertex[0].x - vertex[1].x), 2) + pow((vertex[0].y - vertex[1].y), 2));
            lenth = sqrt(pow((vertex[1].x - vertex[2].x), 2) + pow((vertex[1].y - vertex[2].y), 2));

            bool ok1 = (width < 1.3*lenth) || (lenth < 1.3*width);

            bool ok2 = (width * lenth) >= 100;
            bool ok3 = (width * lenth) <= 300;
            bool ok4 = (width > 2*lenth) || (lenth > 2*width);
            bool ok5 = hierarchy[index][0] !=-1 || hierarchy[index][1] !=-1 ;

            int parent = hierarchy[index][3];
                k= hierarchy[parent][3];
                cout<<k<<endl;
           // cout <<k<<endl;
            //drawContours(img, contours, index, color, NULL, 8, hierarchy);
            Point s1;
            s1.x = (vertex[0].x + vertex[2].x) / 2.0;
            s1.y = (vertex[0].y + vertex[2].y) / 2.0;
            if(hierarchy[index][2]==-1 && hierarchy[index][3]==-1 && ok1 && ok2 &&  ok3){
               // drawContours(img, contours, index, color, NULL, 8, hierarchy);
                circle(img,s1,15,color,2,8,0);
            }
            if(hierarchy[index][3]!=-1 && hierarchy[index][2]==-1 &! ok4){
                drawContours(img, contours, index, Scalar(0,0,255), -1, 8, hierarchy);
            }
        }


        int j = -1;

       /* for (int i = 0; i < contours.size(); i++) {

            vector<Point> points = contours[i];//????????
            RotatedRect box = minAreaRect(Mat(points));//?????¨??2D???????°??×?????????°ü?§????
            Point2f vertex[4];
            box.points(vertex);
            //??????×?????????°ü?§????
            double width, lenth;

            width = sqrt(pow((vertex[0].x - vertex[1].x), 2) + pow((vertex[0].y - vertex[1].y), 2));
            lenth = sqrt(pow((vertex[1].x - vertex[2].x), 2) + pow((vertex[1].y - vertex[2].y), 2));


        }*/
        ::imshow(windowsName, img);
        //::imshow("????", dstimg);

        k = waitKey(33);                //30fps
        if (k == 27) break;    //°???Esc??????·?
    }
}
int main()
{

    Targeting_algorithms();


}